/**
 * Author: ligj@mapbar.com
 * Create Time: 2018/03/14 13:04
 * Description:
 */
import actions from '../actions'
import {
    TAB_VIEW_PUSH,
    TAB_VIEW_SET_TITLE,
    TAB_VIEW_REMOVE,
    TAB_VIEW_ENABLED,
    TAB_VIEW_CLOSE_CURRENT,
    TAB_VIEW_CLOSE_ALL,
    TAB_VIEW_CLOSE_PAGES
} from '../mutations'

const tab_view_remove = function (state, pageConfig) {
    let removePageIdx = state.pages.findIndex(item => item.query['_'] === pageConfig.query['_'])
    let currentPageIdx = state.pages.findIndex(item => item.query['_'] === state.currentPage)
    if (removePageIdx > -1) {
        state.pages.splice(removePageIdx, 1)
    }

    if (removePageIdx < currentPageIdx) {
        state.currentPage = state.pages[--currentPageIdx].query['_']
    } else if (removePageIdx === currentPageIdx) {
        if (removePageIdx > state.pages.length - 1) {
            removePageIdx--
        }
        if (removePageIdx >= 0) {
            state.currentPage = state.pages[removePageIdx].query['_']
        }
    }
}

const moduleModalPages = {
    state: {
        pages: [],
        currentPage: '',
        isEnabled: false
    },
    mutations: {
        [TAB_VIEW_ENABLED] (state, isEnabled) {
            state.isEnabled = isEnabled
        },
        [TAB_VIEW_PUSH] (state, pageConfig) {
            let idx = state.pages.findIndex(item => item.query['_'] === pageConfig.query['_'])
            if (idx === -1) {
                state.pages.push(pageConfig)
            } else {
                // 处理页面参数更新的情况
                // if(pageConfig.fullPath){
                state.pages[idx] = pageConfig
                // }
            }
            state.currentPage = pageConfig.query['_']
        },
        [TAB_VIEW_SET_TITLE] (state, {key, title}) {
            let idx = state.pages.findIndex(item => item.query['_'] === key)
            if (idx > -1) {
                state.pages[idx].title = title
            }
        },
        [TAB_VIEW_REMOVE] (state, pageConfig) {
            tab_view_remove(state, pageConfig)
        },
        [TAB_VIEW_CLOSE_CURRENT] (state, isForceClose) {
            let removePageIdx = state.pages.findIndex(item => item.query['_'] === state.currentPage)
            let closable = state.pages[removePageIdx].meta.closable !== false
            if (!closable && !isForceClose) {
                console.warn('当前页面已被设置为不能关闭')
                return
            }
            if (removePageIdx > -1) {
                state.pages.splice(removePageIdx, 1)
            }

            if (removePageIdx > state.pages.length - 1) {
                removePageIdx--
            }
            if (removePageIdx >= 0) {
                state.currentPage = state.pages[removePageIdx].query['_']
            }
        },
        [TAB_VIEW_CLOSE_ALL] (state, forceClosePages) {
            if (typeof forceClosePages === 'String') {
                forceClosePages = [forceClosePages]
            }
            Object.assign([], state.pages).forEach(item => {
                if (forceClosePages.findIndex(p => p === item.query['_']) > -1) {
                    tab_view_remove(state, item)
                } else {
                    let closable = item.meta.closable !== false
                    if (closable) {
                        tab_view_remove(state, item)
                    }
                }
            })
        },
        [TAB_VIEW_CLOSE_PAGES] (state, closePages) {
            if (typeof closePages === 'String') {
                closePages = [closePages]
            }
            Object.assign([], state.pages).forEach(item => {
                if (closePages.findIndex(p => p === item.query['_']) > -1) {
                    tab_view_remove(state, item)
                }
            })
        }
    },
    actions: {},
    getters: {}
}

export default moduleModalPages
